package com.hzy.nursing.controller.member;

import com.hzy.common.annotation.Log;
import com.hzy.common.core.controller.BaseController;
import com.hzy.common.core.domain.AjaxResult;
import com.hzy.common.core.domain.R;
import com.hzy.common.core.page.TableDataInfo;
import com.hzy.common.enums.BusinessType;
import com.hzy.common.utils.UserThreadLocal;
import com.hzy.nursing.domain.Reservation;
import com.hzy.nursing.dto.ReservationDto;
import com.hzy.nursing.service.IReservationService;
import com.hzy.nursing.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * C端-预约信息Controller
 *
 * @author HZY (根据ruoyi代码和接口文档进行修改)
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags =  "C端-预约信息相关接口")
public class MemberReservationController extends BaseController
{
    @Autowired
    private IReservationService reservationService;

    // Business rule constants
    private static final int MAX_APPOINTMENTS_PER_SLOT = 6;
    private static final int MAX_CANCELLATIONS_PER_DAY = 3;

    /**
     * 查询取消预约数量
     * 接口地址: GET /member/reservation/cancelled-count
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        if (userId == null) {
            return R.fail("无法获取用户信息，请先登录");
        }
        int count = reservationService.getCancelledCount(userId);
        return R.ok(count);
    }

    /**
     * 查询每个时间段剩余预约次数
     * 接口地址: GET /member/reservation/countByTime
     */
    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    // --- 修改点 1: 将参数名从 "date" 修改为 "time" 以匹配接口文档 ---
    public R<List<TimeCountVo>> getCountByTime(@RequestParam("time") Long time){
        List<TimeCountVo> list = reservationService.countReservationsForTime(time);
        return R.ok(list);
    }

    /**
     * 新增预约
     * 接口地址: POST /member/reservation/
     */
    @PostMapping
    @ApiOperation("新增预约")
    @Log(title = "C端新增预约", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody ReservationDto reservationDto) {
        Long userId = UserThreadLocal.getUserId();
        if (userId == null) {
            return error("无法获取用户信息，请先登录");
        }

        // --- Business Validation ---
        if (reservationService.getCancelledCount(userId) >= MAX_CANCELLATIONS_PER_DAY) {
            return error("您今天取消预约次数过多，已无法再次预约");
        }

        long currentBooked = reservationService.countByReservationTime(reservationDto.getTime());
        if (currentBooked >= MAX_APPOINTMENTS_PER_SLOT) {
            return error("该时间段已约满，请选择其他时间");
        }

        return toAjax(reservationService.insertReservation(reservationDto));
    }

    /**
     * 分页查询当前用户的预约信息列表
     * 接口地址: GET /member/reservation/page
     */
    @GetMapping("/page")
    @ApiOperation("分页查询当前用户的预约信息列表")
    public TableDataInfo list(Reservation reservation){
        Long userId = UserThreadLocal.getUserId();
        if (userId == null) {
            return getDataTable(List.of()); // Return empty list if not logged in
        }

        reservation.setCreateBy(String.valueOf(userId));

        startPage(); // startPage 会自动处理请求中的 pageNum 和 pageSize
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return getDataTable(list);
    }

    /**
     * 取消预约
     * 接口地址: PUT /member/reservation/{id}/cancel
     */
    // --- 修改点 2: 将路径从 "/cancel/{id}" 修改为 "/{id}/cancel" ---
    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    @Log(title = "C端取消预约", businessType = BusinessType.UPDATE)
    public AjaxResult cancel(@PathVariable("id") Long id) {
        return toAjax(reservationService.cancelReservation(id));
    }
}